import {HTTP} from "../../../utils/http.js";
var http = new HTTP();
import { FriendModel } from "../../../models/friend.js";
var _FriendModel = new FriendModel();
var _self;
Page({
  data: {
    show: {
      middle: false,
      top: false,
      bottom: false,
      right: false,
    },
    yearList:[],
    yearIndex:0,
    instituteArr: ["计算机科学"],
    instituteIndex: 0,
    subjectArr: ["计算机应用"],
    subjectIndex: 0,
    classArr: ["计维1"],
    classIndex: 0,
    degreeArr: ["专科", "本科", "硕士", "博士", "博士后", "其他"],
    degreeIndex:1,
    userName:"",
    userList:[],
    searching:false
  },
  formSubmit:function(e){
    var params ={};
    var userName = e.detail.value.userName;
    if(userName!=""){
      params.userName=userName
    }
    _FriendModel.searchFriends(params,function(data){
      console.log(data);
       _self.setData({
         userList:data,
         searching:true
       })
    })
  },
  onLoad: function (options) {
    _self = this;
      this.initYear();
      this.initCollegeInfo();
  },
  initCollegeInfo: function () {
    let updata = {
      year: "2013"
    }
    this.getSchooInfo(updata, "instituteArr");

  },
  yearChange:function(e){
    _self.setData({
      yearIndex:e.detail.value
    })

    let updata = {
      year: _self.data.yearList[_self.data.yearIndex]
    }
    this.getSchooInfo(updata, "instituteArr");
  },
  userNameChange:function(e){
    _self.setData({
      userName:e.detail.value
    })
  },
  initYear:function(){
    let yearList = [];
    let yearIndex=0,count=0;
    for (let i = 1980; i <= 2030; i++) {
        yearList.push(i+"");
        if(i==2013){
            yearIndex = count;
        }
        count++;
    }
    this.setData({
        yearList:yearList,
        yearIndex:yearIndex
    })
  },
  filterCancel: function (type){
    this.setData({
      [`show.right`]: false
    });
  },
  filter:function(){
      let params = {}
      let userName =_self.data.userName;
      let year = _self.data.yearList[_self.data.yearIndex];
      let degree = _self.data.degreeArr[_self.data.degreeIndex];
      let college=_self.data.instituteArr[_self.data.instituteIndex];
      let subject = _self.data.subjectArr[_self.data.subjectIndex];
      let className = _self.data.classArr[_self.data.classIndex];
      if (userName != "") {
        params.userName = userName
      }
      if(year != ""){
          params.year = year
      }
      if(degree!=""){
          
      }
      if(college!=undefined){
          params.college = college
      }
      if(subject != undefined){
          params.major = subject;
      }
      if(className != undefined){
          params.dicClass = className
      }
    _FriendModel.searchFriends(params, function (data) {
      _self.setData({
        userList: data,
        searching: true
      })
    })
  },
  toggle:function(type) {
    this.setData({
      [`show.${type}`]: !this.data.show[type]
    });
  },
  toggleRightPopup() {
    this.toggle('right');
  },
  getSchooInfo: function (params, target) {
    http.request({
      url: "/api/getDicDepartment",
      data: params,
      success: function (data) {
        if (data.length == 0) {
          _self.setData({
            [target]: []
          })
        }
        if (data.length > 0) {
          let dataArr = [];
          data.forEach(function (item, index) {
            if (target == 'instituteArr') {
              dataArr.push(item.college);

            }
            if (target == 'subjectArr') {
              dataArr.push(item.major)
            }
            if (target == 'classArr') {
              dataArr.push(item.dicClass)
            }
          });
          _self.setData({
            [target]: dataArr
          })

          if (target == 'instituteArr') {
            let updata = {
              year: _self.data.yearList[_self.data.yearIndex],
              college: _self.data.instituteArr[_self.data.instituteIndex]
            }
            _self.getSchooInfo(updata, "subjectArr");
          }
          if (target == 'subjectArr') {
            let updata = {
              year: _self.data.yearList[_self.data.yearIndex],
              college: _self.data.instituteArr[_self.data.instituteIndex],
              major: _self.data.subjectArr[_self.data.classIndex]
            }
            _self.getSchooInfo(updata, "classArr");
          }
        }else{
          _self.setData({
            instituteArr:[],
            instituteIndex:0,
            subjectArr:[],
            subjectIndex:0,
            classArr:[],
            classIndex:0
          })
        }
      }
    })
  }
})